using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;

public partial class Admin_SanPham_ChiTietSanPham : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
       
            LoadData();
            LoadHinh();
             LoadBL();
        }

    }

    #region"LoadData"
    private void LoadData()
    {
        try
        {
            int id = int.Parse(Request.QueryString["id"]);

            tbSanPham sanPham = tbSanPhamBUS.GetByID(id);
            lit_MaSP.Text = sanPham.MaSP.ToString();
            lit_TenSP.Text = sanPham.TenSP;

            tbDanhMuc danhMuc = tbDanhMucBUS.GetByID(sanPham.DanhMuc);
            lit_DanhMuc.Text = danhMuc.TenDanhMuc;

            tbDoanhNghiep doanhNghiep = tbDoanhNghiepBUS.GetByID(sanPham.DoanhNghiep);
            lit_DoanhNghiep.Text = doanhNghiep.TenDN ;

         
            lit_GiaGoc.Text = sanPham.GiaGoc.ToString();
            lit_GiaBan.Text = sanPham.GiaBan.ToString();
            lit_NgayBatDau.Text = sanPham.NgayBatDau.ToShortDateString();
            lit_NgayHetHan.Text = sanPham.NgayHetHan.ToShortDateString();
            lit_SoLuongBan.Text = sanPham.SoLuongBan.ToString();
            lit_SoLuongToiDa.Text = sanPham.SoLuongToiDa.ToString();
            lit_MoTa.Text = sanPham.MoTa;
            lit_QuiDinhSuDung.Text = sanPham.QuyDinhSuDung;
            lit_ChietKhau.Text = sanPham.ChietKhau.ToString();
            lit_DiaDiemSuDung.Text = sanPham.DiaDiemSuDung;
            if (sanPham.BanDo != "no_images.jpg")
            {
                img_BanDo.ImageUrl =
                "~/Photos/BanDo/Thumb/" + sanPham.BanDo;
            }
            else
            {
                img_BanDo.ImageUrl = "~/Photos/Thumb/no_images.jpg";
            }

            tbTinhTrangSP tinhTrang = tbTinhTrangSPBUS.GetByID(sanPham.TinhTrangSP);
            lit_TinhTrang.Text =tinhTrang.TenTT;

            tbNhanVien nhanVien = tbNhanVienBUS.GetByID(sanPham.NhanVien);
            lit_NhanVien.Text = nhanVien.TenNV;


        }
        catch
        {
            Response.Redirect("~/Admin/SanPham/DanhSachSanPham.aspx");
        }

    }
    #endregion
    #region"LoadHinh"
    private void LoadHinh()
    {
        int id = int.Parse(Request.QueryString["id"]);
        int total = 0;
        pagerHinh.PageSize = 2;
        Criteria c = new Criteria();
        c.add(Condition.AND, tbHinhAnhColumns.MaSP.ToString(),Condition.EQUAL, id);

        tbHinhAnhCollection cthinhAnhCollection = tbHinhAnhBUS.Get(c.Criter,pagerHinh.CurrentIndex,pagerHinh.PageSize,tbHinhAnhColumns.MaHA.ToString(), "ASC", out total);
        Repeater1.DataSource = cthinhAnhCollection;
        Repeater1.DataBind();
        if (Repeater1.Items.Count == 0)
        {
            img_no.Visible = true;
        }
        else
        {
            img_no.Visible = false;
        }
        pagerHinh.ItemCount = total;
    }
    #endregion
    #region"LoadBL"
    private void LoadBL()
    {
        int id = int.Parse(Request.QueryString["id"]);

        pager.PageSize = 5;

        int total = 0;
        Criteria c= new Criteria();
        c.add(Condition.AND, "tbBinhLuan.MaSP", Condition.EQUAL,id);
        tbBinhLuanCollection binhLuanCollection =
           tbBinhLuanBUS.Get(c.Criter, pager.CurrentIndex, pager.PageSize,
            tbBinhLuanColumns.MaBL.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater2.DataSource = binhLuanCollection;
        Repeater2.DataBind();

        pager.ItemCount = total;
    }
    #endregion

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
         e.Item.ItemType == ListItemType.Item)
        {
            tbHinhAnh ctHinhAnh = (tbHinhAnh)e.Item.DataItem;
          
            Image img_HinhAnh = (Image)e.Item.FindControl("img_HinhAnh");
     
         
            img_HinhAnh.ImageUrl = "~/Photos/Thumb/" + ctHinhAnh.Link;


        }
    }
 
    protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
             e.Item.ItemType == ListItemType.Item)
        {
            tbBinhLuan binhLuan = (tbBinhLuan)e.Item.DataItem;
            tbKhachHang khachHang = tbKhachHangBUS.GetByID(binhLuan.MaKH);
            Literal lit_tenKH = (Literal)e.Item.FindControl("lit_tenKH");
            lit_tenKH.Text = khachHang.HoTen;

        }

    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadBL();
    }
    protected void Repeater2_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

    }
    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater2.Items.Count; i++)
        {
            CheckBox cb_Xoa = (CheckBox)Repeater2.Items[i].FindControl("cb_Xoa");
            if (cb_Xoa.Checked == true)
            {
                HiddenField hf_id = (HiddenField)Repeater2.Items[i].FindControl("hf_id");
                int id = int.Parse(hf_id.Value);
                tbBinhLuanBUS.Delete(id);
            }
        }
        Response.Redirect(Request.RawUrl);
    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

    }
    protected void pagerHinh_Command(object sender, CommandEventArgs e)
    {
        pagerHinh.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadHinh();
    }
}
